package com.init.iblog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.init.iblog.domain.ResponseResult;
import com.init.iblog.domain.dto.TagListDto;
import com.init.iblog.domain.entity.Tag;
import com.init.iblog.domain.vo.PageVo;
import com.init.iblog.domain.vo.TagVo;
import com.init.iblog.enums.AppHttpCodeEnum;
import com.init.iblog.exception.SystemException;
import com.init.iblog.mapper.TagMapper;
import com.init.iblog.service.TagService;
import com.init.iblog.utils.BeanCopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 标签(Tag)表服务实现类
 *
 * @author makejava
 * @since 2023-03-02 22:21:50
 */
@Service("tagService")
public class TagServiceImpl extends ServiceImpl<TagMapper, Tag> implements TagService {


    @Autowired(required = false)
    private TagMapper tagMapper;

    /**
     * 查询标签
     * @param pageNum
     * @param pageSize
     * @param tagListDto
     * @return
     */
    @Override
    public ResponseResult<PageVo> pageTagList(Integer pageNum, Integer pageSize, TagListDto tagListDto) {
        //分页查询
        LambdaQueryWrapper<Tag> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.hasText(tagListDto.getName()),Tag::getName,tagListDto.getName());
        queryWrapper.eq(StringUtils.hasText(tagListDto.getRemark()),Tag::getRemark,tagListDto.getRemark());
        Page<Tag> page = new Page<>();
        page.setCurrent(pageNum);
        page.setSize(pageSize);
        page(page,queryWrapper);
        //封装数据返回
        PageVo pageVo = new PageVo(page.getRecords(),page.getTotal());
        return ResponseResult.okResult(pageVo);
    }


    /**
     * 添加便签
     * @param tag
     * @return
     */
    @Override
    public ResponseResult addTag(Tag tag) {

        //标签名不能为空
        if(!StringUtils.hasText(tag.getName())){
            throw new SystemException(AppHttpCodeEnum.CONTENT_NOT_NULL);
        }
        save(tag);

        return ResponseResult.okResult();
    }


    /**
     * 删除标签
     * @param ids
     * @return
     */
    @Override
    public ResponseResult deleteTag(List<Long> ids) {

        //将ids中的所有标签id中的is_deleted字段设置为1(表示逻辑删除)
        LambdaQueryWrapper<Tag> queryWrapper = new LambdaQueryWrapper<>();

        for (Long id: ids) {
            tagMapper.deleteById(id);
        }
        return ResponseResult.okResult();
    }


    /**
     * 获取标签信息
     * @param id
     * @return
     */
    @Override
    public ResponseResult getTag(Long id) {

        Tag tag = tagMapper.selectById(id);

        System.out.println("++++++++++tag" + tag.toString());
        TagVo tagVo = BeanCopyUtils.copyBean(tag, TagVo.class);

        System.out.println("++++++++++tagVo"+tagVo.toString());

        return ResponseResult.okResult(tagVo);
    }

    /**
     * 更新标签信息
     * @param tag
     * @return
     */
    @Override
    public ResponseResult updateTag(Tag tag) {

        tagMapper.updateById(tag);


        return ResponseResult.okResult();
    }

    /**
     * 查询所有标签
     * @return
     */
    @Override
    public List<TagVo> listAllTag() {
        LambdaQueryWrapper<Tag> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(Tag::getId,Tag::getName);
        List<Tag> list = list(wrapper);
        List<TagVo> tagVos = BeanCopyUtils.copyBeanList(list, TagVo.class);
        return tagVos;
    }


}
